package BandB.Tool.GPhoneManager.Widget.AndroidBrightnessControl;

import BandB.Tool.GPhoneManager.R;
import android.app.Activity;
import android.content.ContentResolver; //import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class AndroidBrightnessControl extends Activity {

	private SeekBar brightSeekBar;
	private TextView brightValueTextView;
	private String TAG = "AndroidBrightnessControl";
	private int mBrightnessValue;

	// private Float mPercentBrightnessOf255 = new Float(0);
	// private Context mContext;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brightness_main);

		// mContext = this;

		Log.w(TAG, "onCreate...");
		brightSeekBar = (SeekBar) findViewById(R.id.SeekBar01);
		brightSeekBar.setOnSeekBarChangeListener(seekBarChange);
		brightSeekBar.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
		brightSeekBar.setMax(255);
		brightValueTextView = (TextView) findViewById(R.id.TextView01);
	}

	OnSeekBarChangeListener seekBarChange = new OnSeekBarChangeListener() {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			refreshTextView(seekBar.getProgress());
			if (seekBar.getProgress() < (255 / 10)) {// Minimum value
				seekBar.setProgress((255 / 10));
				refreshTextView(seekBar.getProgress());
			} else {
				setBrightness(seekBar.getProgress());
				mBrightnessValue = seekBar.getProgress();
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

	};

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		if (event.getY() < 163 || event.getY() > 288) {
			finish();
		}

		return super.onTouchEvent(event);
	}

	// set this window brightness
	public void setBrightness(int brightnessValue) {

		WindowManager.LayoutParams p = getWindow().getAttributes();
		Log.w(TAG, "set window brightness " + brightnessValue);
		p.screenBrightness = Float.valueOf(brightnessValue) * (1f / 255f);
		getWindow().setAttributes(p);
	}

	public int getBrightness() {
		int nowBrightnessValue = 0;

		// Uri uri = android.provider.Settings.System
		// .getUriFor("screen_brightness");
		ContentResolver resolver = getContentResolver();

		try {
			nowBrightnessValue = android.provider.Settings.System.getInt(
					resolver, "screen_brightness");
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.w(TAG, "fail to get brightness value");
		}
		Log.w(TAG, "now brightness value is" + nowBrightnessValue);
		return nowBrightnessValue;
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.w(TAG, "onStart...");
		mBrightnessValue = getBrightness();
		brightSeekBar.setProgress(mBrightnessValue);
		refreshTextView(mBrightnessValue);
	}

	protected void refreshTextView(int intNowBrightness) {

		if (intNowBrightness == 255) {
			brightValueTextView.setText("100%");
		} else {
			Float percentFloat = Float.valueOf(intNowBrightness) / 255.f;
			percentFloat = percentFloat + 0.000001f;
			String percentBrightness = String.valueOf(percentFloat);
			percentBrightness = percentBrightness.substring(percentBrightness
					.indexOf(".") + 1, percentBrightness.indexOf(".") + 3);
			percentBrightness = percentBrightness + "%";
			brightValueTextView.setText(percentBrightness);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.w(TAG, "onPause...");

		// Set system brightness
		// ContentObserver myContentObserver = null;
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		ContentResolver resolver = getContentResolver();
		android.provider.Settings.System.putInt(resolver, "screen_brightness",
				mBrightnessValue);
		// resolver.registerContentObserver(uri, true, myContentObserver);
		resolver.notifyChange(uri, null);
		Log.w(TAG, "write settings successfully..." + mBrightnessValue);

		// return results
		// Intent intent = getIntent();
		// Bundle extras = intent.getExtras();
		// int mAppWidgetId = 0;
		// if (extras != null) {
		// mAppWidgetId = extras.getInt(
		// AppWidgetManager.EXTRA_APPWIDGET_ID,
		// AppWidgetManager.INVALID_APPWIDGET_ID);
		// }
		// AppWidgetManager appWidgetManager =
		// AppWidgetManager.getInstance(mContext);
		// RemoteViews views = new
		// RemoteViews(mContext.getPackageName(),R.layout.brightnesscontrolwidget);
		// appWidgetManager.updateAppWidget(mAppWidgetId, views);
		// Intent resultValue = new Intent();
		// resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
		// mAppWidgetId);
		// setResult(RESULT_OK, resultValue);

		this.finish();
	}
}